/*     */ package net.sf.navigator.menu;
/*     */ 
/*     */ import btsm.bean.dbConnector;
/*     */ import java.io.InputStream;
/*     */ import java.io.Serializable;
/*     */ import java.sql.ResultSet;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import java.util.StringTokenizer;
/*     */ import javax.servlet.ServletContext;
/*     */ import net.sf.navigator.displayer.MenuDisplayerMapping;
/*     */ import net.sf.navigator.displayer.VelocityMenuDisplayer;
/*     */ import net.sf.navigator.util.LoadableResource;
/*     */ import net.sf.navigator.util.LoadableResourceException;
/*     */ import org.apache.commons.collections.map.LinkedMap;
/*     */ import org.apache.commons.digester.Digester;
/*     */ import org.apache.commons.logging.Log;
/*     */ import org.apache.commons.logging.LogFactory;
/*     */ 
/*     */ public class MenuRepository
/*     */   implements LoadableResource, Serializable
/*     */ {
/*     */   public static final String MENU_REPOSITORY_KEY = "net.sf.navigator.menu.MENU_REPOSITORY";
/*  42 */   private static Log log = LogFactory.getLog(MenuRepository.class);
/*     */ 
/*  46 */   protected String config = null;
/*  47 */   protected String name = null;
/*  48 */   protected ServletContext servletContext = null;
/*  49 */   protected LinkedMap menus = new LinkedMap();
/*  50 */   protected LinkedMap displayers = new LinkedMap();
/*  51 */   protected LinkedMap templates = new LinkedMap();
/*     */   private String breadCrumbDelimiter;
/*  53 */   private static Map queryMap = new HashMap();
/*  54 */   private dbConnector dbcon = null;
/*     */   private String strUserID;
/*     */ 
/*     */   public Set getMenuNames()
/*     */   {
/*  58 */     return this.menus.keySet();
/*     */   }
/*     */ 
/*     */   public String getStrUserID() {
/*  62 */     return this.strUserID;
/*     */   }
/*     */ 
/*     */   public void setStrUserID(String strUserID) {
/*  66 */     this.strUserID = strUserID;
/*     */   }
/*     */ 
/*     */   public List getTopMenus()
/*     */   {
/*  74 */     List topMenus = new ArrayList();
/*  75 */     if (this.menus == null) {
/*  76 */       log.warn("No menus found in repository!");
/*  77 */       return topMenus;
/*     */     }
/*     */ 
/*  80 */     for (Iterator it = this.menus.keySet().iterator(); it.hasNext(); ) {
/*  81 */       String name = (String)it.next();
/*  82 */       MenuComponent menu = getMenu(name);
/*  83 */       if (menu.getParent() == null) {
/*  84 */         topMenus.add(menu);
/*     */       }
/*     */     }
/*  87 */     return topMenus;
/*     */   }
/*     */ 
/*     */   public MenuComponent getMenu(String menuName) {
/*  91 */     return (MenuComponent)this.menus.get(menuName);
/*     */   }
/*     */ 
/*     */   public MenuDisplayerMapping getMenuDisplayerMapping(String displayerName) {
/*  95 */     return (MenuDisplayerMapping)this.displayers.get(displayerName);
/*     */   }
/*     */ 
/*     */   protected Digester initDigester() {
/*  99 */     Digester digester = new Digester();
/* 100 */     digester.setClassLoader(Thread.currentThread().getContextClassLoader());
/* 101 */     digester.push(this);
/*     */ 
/* 214 */     digester.addObjectCreate("MenuConfig/Displayers/Displayer", "net.sf.navigator.displayer.MenuDisplayerMapping", "mapping");
/* 215 */     digester.addSetProperties("MenuConfig/Displayers/Displayer");
/* 216 */     digester.addSetNext("MenuConfig/Displayers/Displayer", "addMenuDisplayerMapping", "net.sf.navigator.displayer.MenuDisplayerMapping");
/* 217 */     digester.addSetProperty("MenuConfig/Displayers/Displayer/SetProperty", "property", "value");
/*     */ 
/* 220 */     Object objUser = queryMap.get(this.strUserID);
/* 221 */     if (objUser != null)
/* 222 */       doQueryMenu(objUser.toString());
/* 223 */     return digester;
/*     */   }
/*     */ 
/*     */   private void doQueryMenu(String strSQL)
/*     */   {
/* 228 */     removeAllMenus();
/* 229 */     dbConInit();
/*     */     try
/*     */     {
/* 232 */       ResultSet rs = this.dbcon.execQuery(strSQL);
/* 233 */       while (rs.next());
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 238 */       e.printStackTrace();
/*     */     }
/*     */     finally {
/* 241 */       dbConClose();
/*     */     }
/*     */   }
/*     */ 
/*     */   private void dbConInit()
/*     */   {
/* 247 */     if (this.dbcon == null)
/* 248 */       this.dbcon = new dbConnector();
/*     */   }
/*     */ 
/*     */   public void dbConClose() {
/* 252 */     if (this.dbcon != null)
/* 253 */       this.dbcon.closeAll();
/* 254 */     this.dbcon = null;
/*     */   }
/*     */ 
/*     */   public void addMenu(MenuComponent menu)
/*     */   {
/* 261 */     if (this.menus.containsKey(menu.getName())) {
/* 262 */       if (log.isDebugEnabled()) {
/* 263 */         log.warn("Menu '" + menu.getName() + "' already exists in repository");
/*     */       }
/* 265 */       List children = getMenu(menu.getName()).getComponents();
/* 266 */       if ((children != null) && (menu.getComponents() != null)) {
/* 267 */         for (Iterator it = children.iterator(); it.hasNext(); ) {
/* 268 */           MenuComponent child = (MenuComponent)it.next();
/* 269 */           menu.addMenuComponent(child);
/*     */         }
/*     */       }
/*     */     }
/* 273 */     this.menus.put(menu.getName(), menu);
/*     */   }
/*     */ 
/*     */   public void removeMenu(String name)
/*     */   {
/* 281 */     this.menus.remove(name);
/*     */   }
/*     */ 
/*     */   public void removeAllMenus()
/*     */   {
/* 289 */     this.menus.clear();
/*     */   }
/*     */ 
/*     */   public void addMenuDisplayerMapping(MenuDisplayerMapping displayerMapping) {
/* 293 */     this.displayers.put(displayerMapping.getName(), displayerMapping);
/* 294 */     if (displayerMapping.getType().equals("net.sf.navigator.displayer.VelocityMenuDisplayer"))
/* 295 */       if (this.servletContext == null)
/* 296 */         log.error("ServletContext not set - can't initialize Velocity");
/*     */       else
/* 298 */         VelocityMenuDisplayer.initialize(this.servletContext);
/*     */   }
/*     */ 
/*     */   public LinkedMap getDisplayers()
/*     */   {
/* 309 */     return this.displayers;
/*     */   }
/*     */ 
/*     */   public void setDisplayers(LinkedMap displayers)
/*     */   {
/* 319 */     this.displayers = displayers;
/*     */   }
/*     */ 
/*     */   public void load() throws LoadableResourceException {
/* 323 */     if (getServletContext() == null) {
/* 324 */       throw new LoadableResourceException("no reference to servlet context found");
/*     */     }
/*     */ 
/* 327 */     InputStream input = null;
/* 328 */     Digester digester = initDigester();
/*     */     try
/*     */     {
/* 331 */       input = getServletContext().getResourceAsStream(this.config);
/* 332 */       digester.parse(input);
/*     */     } catch (Exception e) {
/* 334 */       e.printStackTrace();
/* 335 */       throw new LoadableResourceException("Error parsing resource file: " + this.config + " nested exception is: " + e.getMessage());
/*     */     } finally {
/*     */       try {
/* 338 */         input.close();
/*     */       } catch (Exception localException1) {
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public void reload() throws LoadableResourceException {
/* 345 */     this.menus.clear();
/* 346 */     this.displayers.clear();
/* 347 */     load();
/*     */   }
/*     */ 
/*     */   public void setLoadParam(String loadParam) {
/* 351 */     this.config = loadParam;
/*     */   }
/*     */ 
/*     */   public String getLoadParam() {
/* 355 */     return this.config;
/*     */   }
/*     */ 
/*     */   public void setName(String name) {
/* 359 */     this.name = name;
/*     */   }
/*     */ 
/*     */   public String getName() {
/* 363 */     return this.name;
/*     */   }
/*     */ 
/*     */   public ServletContext getServletContext() {
/* 367 */     return this.servletContext;
/*     */   }
/*     */ 
/*     */   public void setServletContext(ServletContext context) {
/* 371 */     this.servletContext = context;
/*     */   }
/*     */ 
/*     */   public MenuComponent getMenu(String menuName, String delimiter)
/*     */   {
/* 381 */     MenuComponent parent = null;
/* 382 */     StringTokenizer st = new StringTokenizer(menuName, delimiter);
/* 383 */     boolean firstMenu = true;
/*     */ 
/* 385 */     while (st.hasMoreTokens()) {
/* 386 */       if (firstMenu) {
/* 387 */         parent = getMenu(st.nextToken());
/* 388 */         firstMenu = false;
/*     */       } else {
/* 390 */         MenuComponent child = null;
/* 391 */         String name = st.nextToken();
/* 392 */         for (int a = 0; a < parent.getComponents().size(); a++) {
/* 393 */           if (name.equals(((MenuComponent)parent.getComponents().get(a)).getName())) {
/* 394 */             child = (MenuComponent)parent.getComponents().get(a);
/* 395 */             a = parent.getComponents().size();
/*     */           }
/*     */         }
/* 398 */         if (child != null) {
/* 399 */           parent = child;
/*     */         } else {
/* 401 */           parent = null;
/* 402 */           break;
/*     */         }
/*     */       }
/*     */     }
/*     */ 
/* 407 */     return parent;
/*     */   }
/*     */ 
/*     */   public int getMenuDepth(String menuName)
/*     */   {
/* 418 */     MenuComponent menu = getMenu(menuName);
/* 419 */     if (menu == null)
/* 420 */       return -1;
/* 421 */     if (menu.getMenuComponents() == null)
/* 422 */       return 1;
/* 423 */     return menu.getMenuDepth();
/*     */   }
/*     */ 
/*     */   public int getMenuDepth()
/*     */   {
/* 432 */     int currentDepth = 0;
/*     */ 
/* 434 */     List topMenus = getTopMenus();
/*     */ 
/* 436 */     if (topMenus == null)
/* 437 */       return -1;
/* 438 */     for (Iterator menu = topMenus.iterator(); menu.hasNext(); ) {
/* 439 */       int depth = ((MenuComponent)menu.next()).getMenuDepth();
/* 440 */       if (currentDepth < depth)
/* 441 */         currentDepth = depth;
/*     */     }
/* 443 */     return currentDepth;
/*     */   }
/*     */ 
/*     */   public MenuComponent[] getTopMenusAsArray()
/*     */   {
/* 451 */     List menuList = getTopMenus();
/* 452 */     MenuComponent[] menus = new MenuComponent[menuList.size()];
/* 453 */     for (int a = 0; a < menuList.size(); a++) {
/* 454 */       menus[a] = ((MenuComponent)menuList.get(a));
/*     */     }
/*     */ 
/* 457 */     return menus;
/*     */   }
/*     */ 
/*     */   public List getTopMenusNames()
/*     */   {
/* 465 */     List menus = getTopMenus();
/* 466 */     ArrayList names = new ArrayList();
/* 467 */     for (Iterator iterator = menus.iterator(); iterator.hasNext(); ) {
/* 468 */       MenuComponent menu = (MenuComponent)iterator.next();
/* 469 */       names.add(menu.getName());
/*     */     }
/* 471 */     return names;
/*     */   }
/*     */ 
/*     */   public void setBreadCrumbDelimiter(String string) {
/* 475 */     this.breadCrumbDelimiter = string;
/*     */   }
/*     */ 
/*     */   public void buildBreadCrumbs() {
/* 479 */     if (this.breadCrumbDelimiter == null) {
/* 480 */       throw new NullPointerException("No breadCrumbDelimiter present");
/*     */     }
/* 482 */     ArrayList menus = (ArrayList)getTopMenus();
/* 483 */     for (Iterator iterator = menus.iterator(); iterator.hasNext(); ) {
/* 484 */       MenuComponent menu = (MenuComponent)iterator.next();
/* 485 */       menu.setBreadCrumb(this.breadCrumbDelimiter);
/*     */     }
/*     */   }
/*     */ 
/*     */   public void buildBreadCrumbs(String delimiter) {
/* 490 */     this.breadCrumbDelimiter = delimiter;
/* 491 */     buildBreadCrumbs();
/*     */   }
/*     */ }

/* Location:           E:\java\apache-tomcat-6.0.36\webapps\btsm\WEB-INF\classes\
 * Qualified Name:     net.sf.navigator.menu.MenuRepository
 * JD-Core Version:    0.6.2
 */